// Current.js for PSPTube
// Made By JK108

function Current_CheckURL( url, option )
{
	if (url.match("http://current.com") == "http://current.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Current URL
*/
function Current_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/so.addVariable\(\'assetUrl\', \'(.*?)\'/);
	if(video_url == null){
		var video_url = contents.match(/\<embed wmode\=\"transparent\" src\=\"(.*?)\"/);
		var video_url = YouTube_GetURL(video_url[1].replace("/v/", "/watch?v="));
	} else{
		var video_url = video_url[1];
	}
	return video_url;
}

function Current_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 10;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://current.com/search/0.htm?q=" + keyword.replace(/ /g, "+") + "&loc=false&v=on" );
	} else{
	var num = result.start - 1;
	var contents = GetContents( "http://current.com/search/items/start/" + num + "/q/" + keyword.replace(/ /g, "+") + "/loc/false/s/0/v/1/r/0.htm" );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<h2 class=\"noResults\">NO RESULTS</h2>") == "<h2 class=\"noResults\">NO RESULTS</h2>"){ result.total = 0; return result; }
	var total = contents.match(/\<\/strong\> of \<strong\>(.*?)\<\/strong\>/);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<li class=\"content\">");

	if(pos != -1){
		contents = contents.substring(pos + 20, contents.length);
		//Get URL
		var url = contents.match(/href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Description
		pos = contents.indexOf("<span");
		var description = contents.substring(pos + 5, contents.length);
		pos = description.indexOf("<span");
		var description = description.substring(pos + 5, contents.length);
		pos = description.indexOf("</span>");
		description = description.substring(0, pos);
		description = description.replace(/\<strong style\=\"color\:#000;\"\>/g, "").replace(/ class\=\"fullText\" style\=\"display\: none;\"\>/g, "").replace(/\<br \/\>/g, "");
		if(description.substring(0, 1) == ">"){description = description.substring(1, description.length - 1);}
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(ImproveText(description), 60);
		//Comments
		pos = contents.indexOf(" responses\t\t\t\t\t</a>");
		var comments = contents.substring(0, pos);
		pos = comments.lastIndexOf("\t");
		comments = comments.substring(pos, comments.length);
		var comments = parseInt(comments.replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Current = new Object();
Current.Name          = "Current";		/* Service name */
Current.Description   = "Current";		/* Service description */
Current.SearchDesc    = "Current";		/* Service search screen description */
Current.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Current.CheckURL      = Current_CheckURL;	/* CheckURL function */
Current.GetURL        = Current_GetURL;		/* GetURL function */
Current.Search        = Current_Search;		/* Search function */

// add Current to site list
SiteList.push( Current );
